﻿using System;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Net.Formats.oEmbed
{
    /// <summary>
    ///This is a test class for ResponseFactoryTest and is intended
    ///to contain all ResponseFactoryTest Unit Tests
    ///</summary>
    [TestClass( )]
    public class ResponseFactoryTests
    {
        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion

        [TestMethod]        
        public void CreateLinkResponsePositiveTest( )
        {
            var pars = new Dictionary<string, object>( );
            pars.Add( "type", "link" );
            pars.Add( "author_name", "John Doe" );
            pars.Add( "author_url", "http://www.google.com/" );
            pars.Add( "title", "This resource title" );
            pars.Add( "cache_age", "360" );
            pars.Add( "provider_name", "Test provider" );
            pars.Add( "provider_url", "http://testprovider/oembed" );

            var response = ResponseFactory.Create( pars );

            Assert.IsInstanceOfType( response, typeof( LinkResponse ) );
            Assert.AreEqual( ResponseType.Link, response.Type );
            Assert.AreEqual( pars["author_name"], response.AuthorName );
            Assert.AreEqual( pars["author_url"], response.AuthorUrl.AbsoluteUri );
            Assert.AreEqual( pars["title"], response.Title );
            Assert.AreEqual( pars["cache_age"], response.CacheAge.Value.ToString( ) );
            Assert.AreEqual(pars["provider_name"], response.ProviderName);
            Assert.AreEqual(pars["provider_url"], response.ProviderUrl.AbsoluteUri);
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void CreateLinkResponseNegativeTest( )
        {
            var pars = new Dictionary<string, object>( );
            pars.Add( "type", "link" );
            pars.Add( "author_name", "John Doe" );
            pars.Add( "author_url", "http://www.google.com/" );
            pars.Add( "title", "This resource title" );
            pars.Add( "cache_age", "360" );
            pars.Add( "provider_name", "Test provider" );
            pars.Add( "provider_url", "http://testprovider/oembed" );
            pars.Add( "thumbnail_height", "34" );

            var response = ResponseFactory.Create( pars );

            Assert.IsInstanceOfType( response, typeof( LinkResponse ) );
            Assert.AreEqual( ResponseType.Link, response.Type );
            Assert.AreEqual( pars["author_name"], response.AuthorName );
            Assert.AreEqual( pars["author_url"], response.AuthorUrl.AbsoluteUri );
            Assert.AreEqual( pars["title"], response.Title );
            Assert.AreEqual( pars["cache_age"], response.CacheAge.Value.ToString( ) );
            Assert.AreEqual( pars["provider_name"], response.ProviderName );
            Assert.AreEqual( pars["provider_url"], response.ProviderUrl.AbsoluteUri );
            Assert.AreEqual( pars["thumbnail_height"], response.ThumbnailHeight.Value.ToString( ) );
        }

        [TestMethod]
        public void CreatePhotoResponsePositiveTest( )
        {
            var pars = new Dictionary<string, object>( );
            pars.Add( "type", "photo" );
            pars.Add( "author_name", "John Doe" );
            pars.Add( "author_url", "http://www.google.com/" );
            pars.Add( "title", "This resource title" );
            pars.Add( "cache_age", "360" );
            pars.Add( "provider_name", "Test provider" );
            pars.Add( "provider_url", "http://testprovider/oembed" );
            pars.Add("thumbnail_width", "23");
            pars.Add("thumbnail_height", "45");
            pars.Add("thumbnail_url", "http://testurl/thumnbail.jpg");
            pars.Add("url", "http://testurl/photo.jpg");
            pars.Add("width", "11");
            pars.Add("height", "34");

            var response = ResponseFactory.Create( pars ) as PhotoResponse;

            Assert.IsInstanceOfType( response, typeof( PhotoResponse ) );
            Assert.AreEqual( ResponseType.Photo, response.Type );
            Assert.AreEqual( pars["author_name"], response.AuthorName );
            Assert.AreEqual( pars["author_url"], response.AuthorUrl.AbsoluteUri );
            Assert.AreEqual( pars["title"], response.Title );
            Assert.AreEqual( pars["cache_age"], response.CacheAge.Value.ToString( ) );
            Assert.AreEqual( pars["provider_name"], response.ProviderName );
            Assert.AreEqual( pars["provider_url"], response.ProviderUrl.AbsoluteUri );
            Assert.AreEqual( pars["thumbnail_width"], response.ThumbnailWidth.Value.ToString( ) );
            Assert.AreEqual( pars["thumbnail_height"], response.ThumbnailHeight.Value.ToString( ) );
            Assert.AreEqual( pars["thumbnail_url"], response.ThumbnailUrl.AbsoluteUri );
            Assert.AreEqual( pars["url"], response.Url.AbsoluteUri );
            Assert.AreEqual( pars["width"], response.Width.Value.ToString( ) );
            Assert.AreEqual( pars["height"], response.Height.Value.ToString( ) );
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void CreatePhotoResponseNegativeTest( )
        {
            var pars = new Dictionary<string, object>( );
            pars.Add( "type", "photo" );
            pars.Add( "author_name", "John Doe" );
            pars.Add( "author_url", "http://www.google.com/" );
            pars.Add( "title", "This resource title" );
            pars.Add( "cache_age", "360" );
            pars.Add( "provider_name", "Test provider" );
            pars.Add( "provider_url", "http://testprovider/oembed" );
            pars.Add( "thumbnail_width", "23" );
            pars.Add( "thumbnail_height", "45" );
            pars.Add( "thumbnail_url", "http://testurl/thumnbail.jpg" );
            pars.Add( "width", "11" );
            pars.Add( "height", "34" );

            var response = ResponseFactory.Create( pars ) as PhotoResponse;

            Assert.IsInstanceOfType( response, typeof( PhotoResponse ) );
            Assert.AreEqual( ResponseType.Photo, response.Type );
            Assert.AreEqual( pars["author_name"], response.AuthorName );
            Assert.AreEqual( pars["author_url"], response.AuthorUrl.AbsoluteUri );
            Assert.AreEqual( pars["title"], response.Title );
            Assert.AreEqual( pars["cache_age"], response.CacheAge.Value.ToString( ) );
            Assert.AreEqual( pars["provider_name"], response.ProviderName );
            Assert.AreEqual( pars["provider_url"], response.ProviderUrl.AbsoluteUri );
            Assert.AreEqual( pars["thumbnail_width"], response.ThumbnailWidth.Value.ToString( ) );
            Assert.AreEqual( pars["thumbnail_height"], response.ThumbnailHeight.Value.ToString( ) );
            Assert.AreEqual( pars["thumbnail_url"], response.ThumbnailUrl.AbsoluteUri );
            Assert.IsNull( response.Url );
            Assert.AreEqual( pars["width"], response.Width.Value.ToString( ) );
            Assert.AreEqual( pars["height"], response.Height.Value.ToString( ) );
        }       

        [TestMethod]
        public void CreateRichResponsePositiveTest( )
        {
            var pars = new Dictionary<string, object>( );
            pars.Add( "type", "rich" );
            pars.Add( "author_name", "John Doe" );
            pars.Add( "author_url", "http://www.google.com/" );
            pars.Add( "title", "This resource title" );
            pars.Add( "cache_age", "360" );
            pars.Add( "provider_name", "Test provider" );
            pars.Add( "provider_url", "http://testprovider/oembed" );
            pars.Add( "thumbnail_width", "23" );
            pars.Add( "thumbnail_height", "45" );
            pars.Add( "thumbnail_url", "http://testurl/thumnbail.jpg" );
            pars.Add( "html", @"<strong> hello world</strong>" );
            pars.Add( "width", "11" );
            pars.Add( "height", "34" );

            var response = ResponseFactory.Create( pars ) as RichResponse;

            Assert.IsInstanceOfType( response, typeof( RichResponse ) );
            Assert.AreEqual( ResponseType.Rich, response.Type );
            Assert.AreEqual( pars["author_name"], response.AuthorName );
            Assert.AreEqual( pars["author_url"], response.AuthorUrl.AbsoluteUri );
            Assert.AreEqual( pars["title"], response.Title );
            Assert.AreEqual( pars["cache_age"], response.CacheAge.Value.ToString( ) );
            Assert.AreEqual( pars["provider_name"], response.ProviderName );
            Assert.AreEqual( pars["provider_url"], response.ProviderUrl.AbsoluteUri );
            Assert.AreEqual( pars["thumbnail_width"], response.ThumbnailWidth.Value.ToString( ) );
            Assert.AreEqual( pars["thumbnail_height"], response.ThumbnailHeight.Value.ToString( ) );
            Assert.AreEqual( pars["thumbnail_url"], response.ThumbnailUrl.AbsoluteUri );
            Assert.AreEqual( pars["html"], response.Html );
            Assert.AreEqual( pars["width"], response.Width.Value.ToString( ) );
            Assert.AreEqual( pars["height"], response.Height.Value.ToString( ) );
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void CreateRichResponseNegativeTest( )
        {
            var pars = new Dictionary<string, object>( );
            pars.Add( "type", "rich" );
            pars.Add( "author_name", "John Doe" );
            pars.Add( "author_url", "http://www.google.com/" );
            pars.Add( "title", "This resource title" );
            pars.Add( "cache_age", "360" );
            pars.Add( "provider_name", "Test provider" );
            pars.Add( "provider_url", "http://testprovider/oembed" );           
            pars.Add( "width", "11" );
            pars.Add( "height", "34" );

            var response = ResponseFactory.Create( pars ) as RichResponse;

            Assert.IsInstanceOfType( response, typeof( RichResponse ) );
            Assert.AreEqual( ResponseType.Rich, response.Type );
            Assert.AreEqual( pars["author_name"], response.AuthorName );
            Assert.AreEqual( pars["author_url"], response.AuthorUrl.AbsoluteUri );
            Assert.AreEqual( pars["title"], response.Title );
            Assert.AreEqual( pars["cache_age"], response.CacheAge.Value.ToString( ) );
            Assert.AreEqual( pars["provider_name"], response.ProviderName );
            Assert.AreEqual( pars["provider_url"], response.ProviderUrl.AbsoluteUri );
            Assert.IsNull( response.Html );            
            Assert.AreEqual( pars["width"], response.Width.Value.ToString( ) );
            Assert.AreEqual( pars["height"], response.Height.Value.ToString( ) );
        }

        [TestMethod]
        public void CreateVideoResponsePostiveTest( )
        {
            var pars = new Dictionary<string, object>( );
            pars.Add( "type", "video" );
            pars.Add( "author_name", "John Doe" );
            pars.Add( "author_url", "http://www.google.com/" );
            pars.Add( "title", "This resource title" );
            pars.Add( "cache_age", "360" );
            pars.Add( "provider_name", "Test provider" );
            pars.Add( "provider_url", "http://testprovider/oembed" );
            pars.Add( "thumbnail_width", "23" );
            pars.Add( "thumbnail_height", "45" );
            pars.Add( "thumbnail_url", "http://testurl/thumnbail.jpg" );
            pars.Add( "html", @"<strong> hello world</strong>" );
            pars.Add( "width", "11" );
            pars.Add( "height", "34" );

            var response = ResponseFactory.Create( pars ) as VideoResponse;

            Assert.IsInstanceOfType( response, typeof( VideoResponse ) );
            Assert.AreEqual( ResponseType.Video, response.Type );
            Assert.AreEqual( pars["author_name"], response.AuthorName );
            Assert.AreEqual( pars["author_url"], response.AuthorUrl.AbsoluteUri );
            Assert.AreEqual( pars["title"], response.Title );
            Assert.AreEqual( pars["cache_age"], response.CacheAge.Value.ToString( ) );
            Assert.AreEqual( pars["provider_name"], response.ProviderName );
            Assert.AreEqual( pars["provider_url"], response.ProviderUrl.AbsoluteUri );
            Assert.AreEqual( pars["thumbnail_width"], response.ThumbnailWidth.Value.ToString( ) );
            Assert.AreEqual( pars["thumbnail_height"], response.ThumbnailHeight.Value.ToString( ) );
            Assert.AreEqual( pars["thumbnail_url"], response.ThumbnailUrl.AbsoluteUri );
            Assert.AreEqual( pars["html"], response.Html );
            Assert.AreEqual( pars["width"], response.Width.Value.ToString( ) );
            Assert.AreEqual( pars["height"], response.Height.Value.ToString( ) );
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void CreateVideoResponseNegativeTest( )
        {
            var pars = new Dictionary<string, object>( );
            pars.Add( "type", "video" );
            pars.Add( "author_name", "John Doe" );
            pars.Add( "author_url", "http://www.google.com/" );
            pars.Add( "title", "This resource title" );
            pars.Add( "cache_age", "360" );
            pars.Add( "provider_name", "Test provider" );
            pars.Add( "provider_url", "http://testprovider/oembed" );
            pars.Add( "thumbnail_width", "23" );
            pars.Add( "thumbnail_height", "45" );
            pars.Add( "thumbnail_url", "http://testurl/thumnbail.jpg" );            
            pars.Add( "width", "11" );
            pars.Add( "height", "34" );

            var response = ResponseFactory.Create( pars ) as VideoResponse;

            Assert.IsInstanceOfType( response, typeof( VideoResponse ) );
            Assert.AreEqual( ResponseType.Video, response.Type );
            Assert.AreEqual( pars["author_name"], response.AuthorName );
            Assert.AreEqual( pars["author_url"], response.AuthorUrl.AbsoluteUri );
            Assert.AreEqual( pars["title"], response.Title );
            Assert.AreEqual( pars["cache_age"], response.CacheAge.Value.ToString( ) );
            Assert.AreEqual( pars["provider_name"], response.ProviderName );
            Assert.AreEqual( pars["provider_url"], response.ProviderUrl.AbsoluteUri );
            Assert.AreEqual( pars["thumbnail_width"], response.ThumbnailWidth.Value.ToString( ) );
            Assert.AreEqual( pars["thumbnail_height"], response.ThumbnailHeight.Value.ToString( ) );
            Assert.AreEqual( pars["thumbnail_url"], response.ThumbnailUrl.AbsoluteUri );
            Assert.IsNull( response.Html );
            Assert.AreEqual( pars["width"], response.Width.Value.ToString( ) );
            Assert.AreEqual( pars["height"], response.Height.Value.ToString( ) );
        }
       
    }
}
